package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.Dict;
import cn.com.blueInfo.bpm.system.enums.DictType;
import cn.com.blueInfo.bpm.system.mapper.DictMapper;
import cn.com.blueInfo.bpm.system.repository.DictRepository;
import cn.com.blueInfo.bpm.system.request.DictDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 枚举表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class DictRepositoryImpl
        extends ServiceImpl<DictMapper, Dict>
        implements DictRepository, BaseRepository<Dict, DictDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增枚举
     * @param dict 枚举参数
     */
    @Override
    public Integer createDict(Dict dict) {
        return sqlExecuteResult(save(dict));
    }

    /**
     * 批量新增枚举
     * @param dictList 枚举参数列表
     */
    @Override
    public Integer batchCreateDict(List<Dict> dictList) {
        return sqlExecuteResult(saveBatch(dictList), dictList.size());
    }

    /**
     * 删除枚举
     * @param dictId 枚举ID
     */
    @Override
    public Integer deleteDict(Long dictId) {
        Dict dict = new Dict();
        dict.setId(dictId);
        dict.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(dict));
    }

    /**
     * 批量删除枚举
     *
     * @param dictIdList 枚举ID列表
     */
    @Override
    public Integer batchDeleteDict(List<Long> dictIdList) {
        List<Dict> dictList = new ArrayList<>();
        for (Long dictId : dictIdList) {
            Dict dict = new Dict();
            dict.setId(dictId);
            dict.setDeleted(BaseDeleted.DELETED);
            dictList.add(dict);
        }
        return sqlExecuteResult(removeBatchByIds(dictList), dictList.size());
    }

    /**
     * 更新部分枚举
     * @param dict 枚举参数
     */
    @Override
    public Integer updateDictPartial(Dict dict) {
        validateUpdateId(dict.getId());
        LambdaUpdateWrapper<Dict> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Dict::getId, dict.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部枚举
     * @param dict 枚举参数
     */
    @Override
    public Integer updateDict(Dict dict) {
        validateUpdateId(dict.getId());
        return sqlExecuteResult(updateById(dict));
    }

    /**
     * 查询一条枚举
     * @param dictId 枚举ID
     */
    @Override
    public Dict getDictById(Long dictId) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dict::getId, dictId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询枚举
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public Page<Dict> getDictPage(DictDTO.QueryDTO dictQueryDTO) {
        Page<Dict> dictPage = new Page<>(
                dictQueryDTO.getPage(), dictQueryDTO.getSize());
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictQueryDTO);

        BaseDeleted deleted = dictQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(dictPage, queryWrapper);
        } else {
            return page(dictPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 枚举 数量
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public Long getDictCount(DictDTO.QueryDTO dictQueryDTO) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictQueryDTO);

        BaseDeleted deleted = dictQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 枚举
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public List<Dict> getDictList(DictDTO.QueryDTO dictQueryDTO) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dictQueryDTO);

        BaseDeleted deleted = dictQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<Dict> queryWrapper, DictDTO.QueryDTO dictQueryDTO) {

        String dictCode = dictQueryDTO.getDictCode();
        queryWrapper.eq(StringUtils.isNotBlank(dictCode), Dict::getDictCode, dictCode);

        String dictName = dictQueryDTO.getDictName();
        queryWrapper.eq(StringUtils.isNotBlank(dictName), Dict::getDictName, dictName);

        DictType dictType = dictQueryDTO.getDictType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(dictType), Dict::getDictType, dictType);

        String description = dictQueryDTO.getDescription();
        queryWrapper.eq(StringUtils.isNotBlank(description), Dict::getDescription, description);

        addQueryWrapper(queryWrapper, dictQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<Dict, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                Dict::getOrderNumber,
                Dict::getTenantId,
                Dict::getApplicationId,
                Dict::getStatus,
                Dict::getDeleted,
                Dict::getDeleteUserId,
                Dict::getDeleteTime,
                Dict::getRemark,
                Dict::getCreateUserId,
                Dict::getCreateTime,
                Dict::getUpdateUserId,
                Dict::getUpdateTime,
                Dict::getExtend1,
                Dict::getExtend2,
                Dict::getExtend3);
    }

}
